package com.sx.mvvmlibs.http.tools;

import android.util.Base64;

import com.google.gson.Gson;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: YY
 * @Date: 2021/4/7
 * @Description: 加解密工具类
 * 使用 RSA + AES，实现双向认证
 */
public class EncryptionDecryptUtils {

    private static final String MAP_DATA = "data";
    private static final String MAP_AES_KEY = "aesKey";
    //加密后的数据
    private static String encryptData;
    //加密后的密钥
    private static String encryptAesKey;
    //返将加密后的数据返回
    private static Map<String, Object> encryptMapData;
    //将解密后的数据返回
    private static Map<String, Object> resultMapData;

    /**
     * @Description 加密
     * @param json 需要加密的数据
     * @param priKey 私钥
     * @return
     */
    public static Map<String, Object> encryption(String json, String priKey) {
        String randomKey = AESUtils.getKey();

        try {
            //加密后的数据
            encryptData = AESUtils.encrypt(json, randomKey);
            if (encryptData != null) {
                encryptData = encryptData.replaceAll(" ", "");
            }
            //客户端使用RSA私钥将randomKey加密
            encryptAesKey = Base64.encodeToString(RSAUtils.encryptByPrivateKey(randomKey.getBytes(), priKey), Base64.NO_WRAP);
            if (encryptAesKey != null) {
                encryptAesKey = encryptAesKey.replaceAll(" ", "");
            }

            encryptMapData = new HashMap<>();
            encryptMapData.put(MAP_DATA, encryptData);
            encryptMapData.put(MAP_AES_KEY, encryptAesKey);
            return encryptMapData;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @Description 解密
     * @param encryptAesKey 加密的AES
     * @param encryptData 加密的数据
     * @param pubKey 公钥
     * @return
     */
    public static Map<String, Object> decryptData(String encryptAesKey, String encryptData, String pubKey) {
        try {
            //解密后的AES密钥
            byte[] decryptAesKey = RSAUtils.decryptByPublicKey(Base64.decode(encryptAesKey, Base64.NO_WRAP), pubKey);

            //使用解密后的AES对数据进行解密
            String decryptData = AESUtils.decrypt(encryptData, new String(decryptAesKey, StandardCharsets.UTF_8));
            resultMapData = new HashMap<>();
            resultMapData.put(MAP_DATA, decryptData);
            resultMapData.put(MAP_AES_KEY, decryptAesKey);
            return resultMapData;
        } catch (Exception e) {

            e.printStackTrace();
            return null;
        }
    }

    public static String decryptData(String token, String encryptData) {
        //使用Token对数据进行解密
        try {
            String decryptData = AESUtils.decrypt(encryptData, token);
            return decryptData;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 解密
     * @param aesKey
     * @param data
     * @return
     * @throws Exception
     */
    public static String decrypt(String aesKey, String data, String pubKey) throws Exception {
        byte[] plaintext = RSAUtils.decryptByPublicKey(Base64.decode(aesKey, Base64.NO_WRAP), pubKey);
        String decryptAesKey = new String(plaintext);
        return AESUtils.decrypt(data, decryptAesKey);
    }

    /**
     * 将Map转换成json格式
     * @param map map集合
     * @return
     */
    public static String encodeToJson(Map<String, Object> map) {
        return new Gson().toJson(map);
    }
}
